package com.idbk.chargestation.util;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.idbk.chargestation.AppContext;
import com.idbk.chargestation.amap.activity.GPSNaviActivityMap;

import java.util.Locale;

public class MapUtil {

	public static float calculateDistanceEx(double lat,double lng){				
		double latBegin = AppContext.getInstance().getLat();   //经度
		double lngBegin = AppContext.getInstance().getLng();   //纬度

		//调用地图API 的AMapUtils对象的calculateLineDistance方法 自动获的距离		
		return AMapUtils.calculateLineDistance(
				new LatLng(latBegin, lngBegin), 
				new LatLng(lat,lng));
	}

	public static String calculateDistance(double lat,double lng){		
		String result;
		double latBegin = AppContext.getInstance().getLat();   //经度
		double lngBegin = AppContext.getInstance().getLng();   //纬度

		//调用地图API 的AMapUtils对象的calculateLineDistance方法 自动获的距离
		float distance = AMapUtils.calculateLineDistance(
				new LatLng(latBegin, lngBegin), 
				new LatLng(lat,lng));
		if(distance > 1000) {
			//如果距离大与1000 显示km，否则显示m
			result = String.format(Locale.getDefault(),"%.1f km", distance / 1000f);
		} else {
			result = String.format(Locale.getDefault(),"%d m", (int)distance);
		}
		return result;
	}

	//开始导航
	public static void startNavi(
			final Context context,
			final double destLat,
			final double destLng) {
		final double beginLat = AppContext.getInstance().getLat();
		final double beginLng = AppContext.getInstance().getLng();
		if(appInstalledOrNot(context,"com.autonavi.minimap")){
			String[] strs = {"APP内导航","高德地图导航[推荐]"};
			new AlertDialog.Builder(context)
					.setItems(strs, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							if(which == 0){
								goToAmapSDKNav(context,beginLat,beginLng,destLat,destLng);
							} else {
								goToAmapAppNav(context,destLat,destLng);
							}
						}
					})
					.create()
					.show();
		} else {
			goToAmapSDKNav(context,beginLat,beginLng,destLat,destLng);
		}
	}

	private static void goToAmapSDKNav(Context context,double beginLat,double beginLng,double destLat,double destLng){
		Intent gpsNaviIntent = new Intent(context, GPSNaviActivityMap.class);
		//传入 起始地址
		Bundle bundle = new Bundle();
		bundle.putDouble(Const.NAVI_START_LAT, AppContext.getInstance().getLat());
		bundle.putDouble(Const.NAVI_START_LNG, AppContext.getInstance().getLng());
		bundle.putDouble(Const.NAVI_STOP_LAT, destLat);
		bundle.putDouble(Const.NAVI_STOP_LNG, destLng);
		gpsNaviIntent.putExtras(bundle);
		gpsNaviIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		context.startActivity(gpsNaviIntent);
	}

	private static void goToAmapAppNav(Context context,double destLat,double destLng){
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_VIEW);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		//将功能Scheme以URI的方式传入data
		//sourceApplication 第三方调用应用名称。如 amap
		//poiname POI 名称
		//dev 是否偏移(0:lat 和 lon 是已经加密后的,不需要国测加密; 1:需要国测加密)
		//style 导航方式(0 速度快; 1 费用少; 2 路程短; 3 不走高速；4 躲避拥堵；5 不走高速且避免收费；6 不走高速且躲避拥堵；7 躲避收费和拥堵；8 不走高速躲避收费和拥堵)
		String su = String.format(Locale.CHINA,
				"androidamap://navi?sourceApplication=%s&lat=%f&lon=%f&dev=1&style=2",
				"chargestation",
				destLat,
				destLng);
		Uri uri = Uri.parse(su);
		intent.setPackage("com.autonavi.minimap");
		intent.setData(uri);
		//启动该页面即可
		context.startActivity(intent);
	}

	private static boolean appInstalledOrNot(Context context,String packageName) {
		PackageManager pm = context.getPackageManager();
		try {
			pm.getPackageInfo(packageName,PackageManager.GET_ACTIVITIES);
			return true;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return false;
	}


}
